package com.orchard.xpz.service.plantFood.Impl;

import com.github.pagehelper.PageHelper;
import com.orchard.xpz.dao.PlantFoodDao;
import com.orchard.xpz.entity.GardenPlot.GardenPlot;
import com.orchard.xpz.entity.plantFood.PlantFood;
import com.orchard.xpz.service.plantFood.PlantFoodService;
import com.orchard.xpz.utils.PageResultData;
import com.orchard.xpz.utils.UserRuntime;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * @TaskCode:
 * @description:
 * @author: minshuai.chen
 * @create: 2022/4/19 15:18
 **/
@Service
public class PlantFoodServiceImpl implements PlantFoodService {

    @Autowired
    private PlantFoodDao plantFoodDao;

    @Override
    public PageResultData<PlantFood> getPageList(PlantFood plantFood) {
        PageHelper.startPage(plantFood.getPageNum(),plantFood.getPageSize());
        List<PlantFood> list = plantFoodDao.getList(plantFood);
        PageResultData<PlantFood> pageResultData = new PageResultData<>(list, plantFood.getPageNum(), plantFood.getPageSize());
        return pageResultData;
    }

    @Override
    public boolean saveOrUpdate(PlantFood plantFood) {
        if (ObjectUtils.isEmpty(plantFood.getPlantId())){
            plantFood.setCreateName(UserRuntime.getUser().getUserName());
            plantFood.setCreateTime(LocalDate.now());
            int i = plantFoodDao.insertSelective(plantFood);
            return i > 0? true : false;
        }else {
            plantFood.setUpdateTime(LocalDate.now());
            plantFood.setUpdateName(UserRuntime.getUser().getUserName());
            int i = plantFoodDao.updateByPrimaryKeySelective(plantFood);
            return i > 0? true : false;
        }
    }

    @Override
    public boolean deltePlot(String id) {
        return plantFoodDao.deleteByPrimaryKey(id) > 0? true : false;
    }

    @Override
    public List<PlantFood> getCountList(PlantFood plantFood) {
        return plantFoodDao.getCountList(plantFood);
    }
}
